package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductPanelGoodsService;
import com.sun.mail.imap.protocol.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @author FuHao
 * @version 1.0.0
 * @description TODO
 * @date 2022/6/18 9:51
 */
@Service
public class IProductPanelGoodsServiceImpl implements IProductPanelGoodsService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;


    /**
     * 完成购物商城主页面显示
     * @return PanelDtoResponse
     * @author FuHao
     * @since 2022/06/18 9:52
     */
    @Override
    public HomePageResponse IProductPanelHomepage() {

        // List<PanelContentItem> panelContents = panelContentMapper.selectPanelContentAndProductWithPanelId();
        List<PanelDto> panel = panelMapper.selectPanelContentById();


        for (PanelDto panelDto : panel) {
            Integer panelId = panelDto.getId();
            List<PanelContentItemDto> panelDtos = panelContentMapper.selectPanelContentAndProductWithPanelId(panelId);

            panelDto.setPanelContentItems(panelDtos);
        }

        HomePageResponse homePageResponse = new HomePageResponse();

        try {
            // 成功执行
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            homePageResponse.setPanelContentItemDtos(panel);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(homePageResponse, e);
        }

        return homePageResponse;

    }



    /**
     * 实现显示导航栏功能
     * @param
     * @return
     * @author FuHao
     * @since 2022/06/19 12:29
     */
    @Override
    public NavListResponse showNavigation() {

        List<PanelContentDto> navigationList = panelContentMapper.queryPanelContentNavigation();

        NavListResponse navListResponse = new NavListResponse();

        try {
            navListResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            navListResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            navListResponse.setPannelContentDtos(navigationList);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(navListResponse, e);
        }


        return navListResponse;
    }

    /**
     * 实现目录显示
     * @return CategoriesResponse
     * @author FuHao
     * @since 2022/06/19 12:55
     */
    @Override
    public CategoriesResponse showCategories() {

        List<ProductCateDto> productCateDtos = panelMapper.queryProductCategories();

        CategoriesResponse categoriesResponse = new CategoriesResponse();

        try {
            // 成功执行
            categoriesResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            categoriesResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            categoriesResponse.setProductCateDtoList(productCateDtos);

        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(categoriesResponse, e);
        }

        return categoriesResponse;
    }


}
